package com.net.xpay.core.helper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @author  on 03/02/2018.
 * 获取当前是开发还是线上环境
 */
@Component
public class EnvironmentHelper {
    private static final String LOCAL = "local"; //本地
    private static final String DEV = "dev";  //开发
    private static final String TEST = "test"; //测试


    @Autowired
    private Environment environment;

    public boolean isDev() {
        return Arrays.stream(environment.getActiveProfiles()).anyMatch(
                DEV::equalsIgnoreCase
        );
    }

    public boolean isTest() {
        return Arrays.stream(environment.getActiveProfiles()).anyMatch(
                TEST::equalsIgnoreCase
        );
    }

    public boolean isDevOrLocal() {
        return Arrays.stream(environment.getActiveProfiles()).anyMatch(
                p -> LOCAL.equalsIgnoreCase(p) || DEV.equalsIgnoreCase(p)
                        || TEST.equals(p)
        );
    }

    public boolean isProd() {
        return !isDevOrLocal();
    }

    public String getEnv() {
        return environment.getActiveProfiles()[0];
    }

}
